// 1创建mongooes数据库连接（注意数据库地址）
const mongoose = require('mongoose')
mongoose.connect('mongodb://node:123456@114.132.75.251:27017/nodeTest').then(r=>{
    console.log('数据库连接成功')
});

// 2创建nodeJS服务
const http = require('http');
const url = require('url')

//4引用用户数模型
const USERS = require('./model/users')

http.createServer(async function (req, res){
    res.writeHead(200, {'Content-Type': 'application/json'});

    // 解析URL参数
    const urlObj = url.parse(req.url ,true);
    const query = urlObj.query;
    console.log(query)
    if(query && query.name && query.age){
        let data = await USERS.create(query)
        res.end(JSON.stringify(data));
    }else {
        res.end(JSON.stringify([{msg:'参数错误，不能存储'}]))
    }




}).listen(1337, '172.25.252.65');

console.log('Server running at http://172.25.252.65:1337/');